//Copyright (C) 2009 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

/*
 * MaterialsDialog.java
 *
 * Created on Oct 18, 2009, 7:52:04 PM
 */

package org.workplicity.marist.grounds.ui;

import org.workplicity.marist.grounds.GroundsRequest;
import org.workplicity.marist.grounds.util.GroundsDate;
import java.util.Calendar;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.workplicity.util.Helper;
import org.workplicity.util.Resources;
import org.workplicity.util.WorkDate;

/**
 *
 * @author Ron Coleman
 */
public class MaterialsDialog extends javax.swing.JDialog {
    private static MaterialsDialog dialog = null;

    private final GroundsRequest order;

    private String initialCompleteDateText = "";

    /** Creates new form MaterialsDialog */
    public MaterialsDialog(JDialog parent, GroundsRequest order) {
        super(parent, true);
        initComponents();

        this.order = order;

        this.setTitle("Grounds - Materials");

        this.setIconImage(Resources.logoSmall.getImage());
        
        this.setLocationRelativeTo(parent);
        
        dialog = this;

        initWithOrder();
    }

    private void initWithOrder() {
        this.materialsTextArea.setText(order.getMaterials());

         if(order.getCompleteDate() != null) {
            GroundsDate date = new GroundsDate(order.getCompleteDate());

            this.completeDateTextField.setText(date.toString());
        }

        // Set initial complete date text to detect changes on save
        initialCompleteDateText = this.completeDateTextField.getText();

        this.estHrsTextField.setText(order.getEstHours()+"");
        this.actHrsTextField.setText(order.getActHours()+"");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        materialsTextArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        completeDateTextField = new javax.swing.JTextField();
        calendarButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        estHrsTextField = new javax.swing.JTextField();
        actHrsTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setFont(new java.awt.Font("Dialog", 0, 14));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Materials:");

        materialsTextArea.setColumns(20);
        materialsTextArea.setRows(5);
        jScrollPane1.setViewportView(materialsTextArea);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("Completion date:");

        completeDateTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        completeDateTextField.setText("mm / dd / yyyy");

        calendarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cal.png"))); // NOI18N
        calendarButton.setToolTipText("Click to open calendar.");
        calendarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calendarButtonActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("Est. hrs:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setText("Act. hrs:");

        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setFont(new java.awt.Font("Tahoma", 0, 14));
        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        estHrsTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        estHrsTextField.setText("0.0");

        actHrsTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        actHrsTextField.setText("0.0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(54, 54, 54)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(completeDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(estHrsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(actHrsTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(calendarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))))
                        .addGap(19, 19, 19))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(completeDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(calendarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(estHrsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(actHrsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void calendarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calendarButtonActionPerformed

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                WorkDate date = Helper.toDate(completeDateTextField.getText());
                CalendarDialog3 calendar = new CalendarDialog3(dialog,date);

                calendar.setVisible(true);
                if(calendar.getCalendar() == null)
                    return;

                int year = calendar.getCalendar().get(Calendar.YEAR);
                int mon = calendar.getCalendar().get(Calendar.MONTH) + 1;
                int day = calendar.getCalendar().get(Calendar.DAY_OF_MONTH);

                String mons = mon < 10 ? "0"+mon : mon+"";
                String days = day < 10 ? "0"+day : day+"";

                dialog.completeDateTextField.setText(mons + " / " + days + " / "+year);
            }
        });
    }//GEN-LAST:event_calendarButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String curCompleteDateText = this.completeDateTextField.getText();

        if (!this.initialCompleteDateText.equals(curCompleteDateText)) {
            WorkDate date = Helper.toDate(curCompleteDateText);

            if (date == null) {
                JOptionPane.showMessageDialog(this, "Bad complete date: must be MM/DD/YYYY",
                        "Grounds", JOptionPane.ERROR_MESSAGE);
                return;
            }

            order.setCompleteDate(date);
        }

        try {
            order.setEstHours(Double.parseDouble(this.estHrsTextField.getText()));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Bad est. hrs.: must be a number",
                    "Grounds", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            order.setActHours(Double.parseDouble(this.actHrsTextField.getText()));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Bad act. hrs.: must be a number",
                    "Grounds", JOptionPane.ERROR_MESSAGE);
            return;
        }

        order.setMaterials(this.materialsTextArea.getText());

        dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField actHrsTextField;
    private javax.swing.JButton calendarButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField completeDateTextField;
    private javax.swing.JTextField estHrsTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea materialsTextArea;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
